package com.imooc.service.impl;

import com.imooc.enums.CommentLevel;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.*;
import com.imooc.pojo.*;
import com.imooc.pojo.VO.ItemCommentVO;
import com.imooc.pojo.VO.ItemsCommentsLevelCountVO;
import com.imooc.pojo.VO.SearchItemVO;
import com.imooc.pojo.VO.ShopcartVO;
import com.imooc.service.ItemsService;
import com.imooc.utils.DesensitizationUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author lihongyu
 * @CreateTime 2021-10-13 14:07
 * @Description 商品业务逻辑实现类
 */
@Service
public class ItemsServiceImpl implements ItemsService {

    @Resource
    private ItemsMapper itemsMapper;

    @Resource
    private ItemsImgMapper itemsImgMapper;

    @Resource
    private ItemsSpecMapper itemsSpecMapper;

    @Resource
    private ItemsParamMapper itemsParamMapper;

    @Resource
    private ItemsCommentsMapper itemsCommentsMapper;

    @Resource
    private ItemsMapperCustom itemsMapperCustom;

    /**
     * 根据商品id获取商品对象
     *
     * @param itemId 商品id
     * @return 商品对象信息
     */
    @Override
    public Items getItemsById(String itemId) {
        return itemsMapper.selectByPrimaryKey(itemId);
    }

    /**
     * 根据商品id获取商品图片列表
     *
     * @param itemId 商品Id
     * @return 商品图片信息列表
     */
    @Override
    public List<ItemsImg> getItemsImgListByItemId(String itemId) {
        Example example = new Example(ItemsImg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);
        return itemsImgMapper.selectByExample(example);
    }

    /**
     * 根据商品id获取商品规格列表
     *
     * @param itemId 商品id
     * @return 商品规格信息列表
     */
    @Override
    public List<ItemsSpec> getItemsSpecListByItemId(String itemId) {
        Example example = new Example(ItemsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);
        return itemsSpecMapper.selectByExample(example);
    }

    /**
     * 扣减商品规格中的库存信息
     *
     * @param itemSpecId 商品规格id
     * @param buyCounts  购买数量
     */
    @Override
    public void decreaseItemSpecStock(String itemSpecId, int buyCounts) {
        int result = itemsMapperCustom.decreaseItemSpecStock(itemSpecId, buyCounts);
        if (result != 1) {
            throw new RuntimeException("订单创建失败，原因：库存不足!");
        }
    }

    /**
     * 根据商品id获取商品主图
     *
     * @param itemId 商品id
     * @return 商品主图路径
     */
    @Override
    public String getItemMainImgById(String itemId) {
        ItemsImg itemsImg = new ItemsImg();
        itemsImg.setItemId(itemId);
        itemsImg.setIsMain(YesOrNo.YES.type);
        ItemsImg result = itemsImgMapper.selectOne(itemsImg);
        return result != null ? result.getUrl() : "";
    }

    /**
     * 根据商品规格id获取商品信息
     *
     * @param specId 商品规格id
     * @return 商品信息
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ItemsSpec queryItemSpecById(String specId) {
        return itemsSpecMapper.selectByPrimaryKey(specId);
    }

    /**
     * 根据商品规格id列表获取最新商品信息
     *
     * @param itemSpecIds 商品规格id列表
     * @return 最新商品信息列表
     */
    @Override
    public List<ShopcartVO> queryItemsBySpecIds(String itemSpecIds) {
        String[] ids = itemSpecIds.split(",");
        List<String> specIdsList = new ArrayList<>();
        Collections.addAll(specIdsList, ids);
        return itemsMapperCustom.queryItemsBySpecIds(specIdsList);
    }

    /**
     * 根据分类id搜索商品
     *
     * @param catId 分类id
     * @param sort  排序规则
     * @return 商品列表信息
     */
    @Override
    public List<SearchItemVO> searchItemsListByCatId(String catId, String sort) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("catId",catId);
        paramMap.put("sort",sort);
        return itemsMapperCustom.searchItemsListByCatId(paramMap);
    }

    /**
     * 搜索商品列表
     *
     * @param keywords 关键字
     * @param sort     排序规则
     * @return 商品列表
     */
    @Override
    public List<SearchItemVO> searchItemsList(String keywords, String sort) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywords",keywords);
        paramMap.put("sort",sort);
        return itemsMapperCustom.searchItemsList(paramMap);
    }

    /**
     * 根据商品id与评价级别获取评价列表
     *
     * @param itemId       商品id
     * @param commentLevel 评价级别(非必填)
     * @return 商品评价列表信息
     */
    @Override
    public List<ItemCommentVO> getItemsCommentListByItemIdAndCommentLevel(String itemId, Integer commentLevel) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("itemId",itemId);
        paramMap.put("commentLevel",commentLevel);
        List<ItemCommentVO> itemsCommentList = itemsMapperCustom.getItemsCommentList(paramMap);
        itemsCommentList.forEach(itemCommentVO -> itemCommentVO.setNickname(DesensitizationUtil.commonDisplay(itemCommentVO.getNickname())));
        return itemsCommentList;
    }

    /**
     * 根据商品id获取商品评价总记录数
     *
     * @param itemId 商品id
     * @return 商品评价总记录数
     */
    @Override
    public ItemsCommentsLevelCountVO getItemsCommentLevelCountByItemId(String itemId) {
        Integer totalCounts = getCommentLevelCount(itemId, null);
        Integer goodCounts = getCommentLevelCount(itemId, CommentLevel.GOOD.type);
        Integer normalCounts = getCommentLevelCount(itemId, CommentLevel.NORMAL.type);
        Integer badCounts = getCommentLevelCount(itemId, CommentLevel.BAD.type);
        ItemsCommentsLevelCountVO itemsCommentsLevelCountVO = new ItemsCommentsLevelCountVO();
        itemsCommentsLevelCountVO.setTotalCounts(totalCounts);
        itemsCommentsLevelCountVO.setGoodCounts(goodCounts);
        itemsCommentsLevelCountVO.setNormalCounts(normalCounts);
        itemsCommentsLevelCountVO.setBadCounts(badCounts);
        return itemsCommentsLevelCountVO;
    }

    /**
     * 获取商品评价总记录数
     *
     * @param itemId 商品ID
     * @param commentLevel 评价等级
     * @return 商品评价总记录数
     */
    private Integer getCommentLevelCount(String itemId, Integer commentLevel) {
        ItemsComments itemsComments = new ItemsComments();
        itemsComments.setItemId(itemId);
        if(commentLevel != null){
            itemsComments.setCommentLevel(commentLevel);
        }
        return itemsCommentsMapper.selectCount(itemsComments);
    }

    /**
     * 根据商品id获取商品参数信息
     *
     * @param itemId 商品id
     * @return 商品参数信息
     */
    @Override
    public ItemsParam getItemsParamsByItemId(String itemId) {
        Example example = new Example(ItemsParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);
        return itemsParamMapper.selectOneByExample(example);
    }
}
